<?php

namespace Inscripcion\Model\Dao;

/**
 * Componentes necesarios para el modelado
 */
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;
use Inscripcion\Model\Entity\ControlInscripcion;
use Inscripcion\Model\Dao\GenericDAO;

/**
 * @package Dao
 */
class ControlInscripcionDAO extends GenericDAO {

    protected $dbAdapter;

    /**
     * Nombre de la Tabla
     * @var type 
     */
    protected $tabla = 'control_inscripcion';
    protected $vista1 = 'control_inscripcion_por_cedula_y_lapso';
    private static $controlInscripcionDao;

    /**
     * Configura Adaptador de Base de Datos
     * 
     * @param \Zend\Db\Adapter\Adapter $adapter
     */
    private function __construct(Adapter $adapter) {
        $this->dbAdapter = $adapter;
    }

    /**
     * 
     * @param type $adapter
     * @return instancia de ControlINscripcionDao
     * Patron singleton
     * PROBADO
     */
    public static function getInstance($adapter) {
        if (self::$controlInscripcionDao == null) {
            self::$controlInscripcionDao = new ControlInscripcionDao($adapter);
        }
        return self::$controlInscripcionDao;
    }

    public function getRegistro($id) {
        return $this->getRegistroGeneral($id, $this->tabla, $this->dbAdapter);
    }

//    public function getControlInscripcion($id) {
//        $sql = new Sql($this->dbAdapter);
//        $select = $sql->select();
//        $select->from($this->tabla);
//        $select->where("id='" . $id . "'");
//        $selectString = $sql->getSqlStringForSqlObject($select);
//        $adapter = $this->dbAdapter;
//        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
//        return $results;
//    }

    /**
     * 
     */
    public function getControlInscripcionsSegunCedulaYLapso($cedulaAlumno, $lapso) {
        $sql = new Sql($this->dbAdapter);
        $select = $sql->select();
        $select->from($this->vista1);
        $select->where(array("cedula = " . $cedulaAlumno . "",
            "id_lapso = " . $lapso));
        $selectString = $sql->getSqlStringForSqlObject($select);
        $adapter = $this->dbAdapter;
        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
        return $results;
    }

    public function getControlInscripcionSegunFecha($fecha, $lapso) {
//        $sql = new Sql($this->dbAdapter);
//        $select = $sql->select();
//        $select->from($this->tabla);
//        $select->where(array("cast(fecha_inicio as date) = '" . $fecha . "'", 'id_lapso' => $lapso, 'id_metodologia' => array(1, 2)));
//        $select->order(array('fecha_inicio', 'id_alumno'));
//        $selectString = $sql->getSqlStringForSqlObject($select);
//        $adapter = $this->dbAdapter;
//        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
//        return $results;
    }

    public function getControlInscripcionEstudiantesMetodologiaPresencial($lapso) {
//        $sql = new Sql($this->dbAdapter);
//        $select = $sql->select();
//        $select->from($this->tabla);
//        $select->where(array("id_metodologia <> 1 ", "id_metodologia <> 2 ", 'id_lapso' => $lapso));
//        $select->order(array('id', 'id_alumno'));
//        $selectString = $sql->getSqlStringForSqlObject($select);
//        $adapter = $this->dbAdapter;
//        $results = $adapter->query($selectString, $adapter::QUERY_MODE_EXECUTE);
//        return $results;
    }

     public function updateCodigoSeguridad($id, $codigo, $fecha) {
        $sql = new Sql($this->dbAdapter);
        $update = $sql->update();
        $update->table($this->tabla);
        $update->set(array('codigo_seguridad' => $codigo, 'fecha_inscripcion' => $fecha));
        $update->where(array('id' => $id));
        $statement = $sql->prepareStatementForSqlObject($update);
        //echo $sql->getSqlStringForSqlObject($update);
        if ($statement->execute()) {
            return true;
        } else {
            return false;
        }
    }

}

